<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>求质数</title>
</head>
<body>
	<h1>求质数</h1><hr>
	<p></p>
	<form id="form1" name="form1">
		<input type="text" id="Num1" name="Num1" size=="20" value="数值">
		<input type="button" value="计算" onclick="NumSubmit();">
	</form>
	<script type="text/javascript">	
		function NumSubmit() {
			InputNum = document.form1.Num1.value;
			if(isNaN(InputNum)) {
				alert("不是有效数值");
			} else {
				if(InputNum == "") {
					alert("不能为空");
				} else {
					if(InputNum < 2) {
						alert("数值太小");
					} else {
						showPrimeNumber(InputNum);
					}
				}
			}
		}
		
		function showPrimeNumber(max) {
			var max, j;
			document.write("小于" + max + "的质数有: <br><br>");
			var Num = new Array(max);
			for(var i=0; i<max; i++) {
				Num[i] = i;
			}
			Num[0] = NaN;
			Num[1] = NaN;
			for(i=2; i<Math.sqrt(max); i++) {
				for(j=i; j<=(max/i); j++) {
					Num[i*j] = NaN;
				}
			}
			for(var i=0; i<max; i++) {
				if(!isNaN(Num[i])) {
					document.write(Num[i] + " ");
				}
			}

		}
	</script>

</body>
</html>